#include "InStreamImpl.h"


InStreamImpl::InStreamImpl(void)
{
	m_mid_Read = 0;
	m_mid_Rewind = 0;
}

InStreamImpl::~InStreamImpl(void)
{
}

bool InStreamImpl::init_jni(JNIEnv* env)
{
	bool result = false;

	do 
	{
		m_mid_Read = (env)->GetMethodID(m_cls, "read", "([B)I");
		if(!m_mid_Read)
			break;

		m_mid_Rewind = (env)->GetMethodID(m_cls, "rewind", "()I");
		if(!m_mid_Rewind)
			break;

		result = true;

	} while (false);

	return result;
}

int InStreamImpl::Read(void *buf,int len)
{
	int result = 0;
	JNIEnv* env = 0;
	if(before(&env))
	{
		jbyteArray array = JniHelper::c2j_bytes(env, 0, len);
		result = (env)->CallIntMethod(m_obj, m_mid_Read, array);
		JniHelper::j2c_bytes(env, array, (unsigned char*)buf, len);

		after(env);
	}
	return result;
}

int InStreamImpl::Rewind()
{
	int result = 0;
	JNIEnv* env = 0;
	if(before(&env))
	{
		result = (env)->CallIntMethod(m_obj, m_mid_Rewind);

		after(env);
	}
	return result;
}